package com.woniu.controller;

import com.woniu.entity.AllUser;
import com.woniu.entity.dto.LoginDTO;
import com.woniu.service.AllUserService;
import com.woniu.service.SmsService;
import com.woniu.util.ResponseData;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * @ClassName SmsController
 * @Description 短信测试
 * @Author nanqiao
 * @Date 2025/6/13 10:52
 * @Version 1.0
 */

@RestController
@RequestMapping("/sms")
@Slf4j
public class SmsController {
    @Resource
    private AllUserService allUserService;

    @Resource
    private SmsService smsService;

    @Resource
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 发送短信验证码
     *@Author nanqiao
     * @param phoneNumber 手机号码
     * @return 响应数据
     */
    @PostMapping("/send-sms-code")
    public ResponseData<String> sendCode(@RequestParam String phoneNumber) {
        String code = String.valueOf((int) ((Math.random() * 9 + 1) * 100000));
        boolean success = smsService.sendVerificationCode(phoneNumber, code);

        if (success) {
            redisTemplate.opsForValue().set("SMS_" + phoneNumber, code, 5, TimeUnit.MINUTES);
            System.out.println("验证码=====================：" + code);
            return ResponseData.ok("验证码发送成功");
        } else {
            return ResponseData.fail(400, "验证码发送失败");
        }
    }

    /**
     * 验证短信验证码
     *@Author nanqiao
     * @param phoneNumber 手机号码
     * @param inputCode   输入的验证码
     * @return 响应数据
     */
    /**
     * 短信登录
     * @param phoneNumber
     * @param inputCode
     * @return
     */
    @PostMapping("/verify-sms-login")
    public ResponseData<String> verifyCodelogin(@RequestParam String phoneNumber, @RequestParam String inputCode) {
        String key = "SMS_" + phoneNumber;
        String storedCode = redisTemplate.opsForValue().get(key);
        if (storedCode == null) {
            return ResponseData.fail(400, "验证码已过期或不存在");
        }
        if (!storedCode.equals(inputCode)) {
            return ResponseData.fail(400, "验证码不正确");
        }
        //通过电话获取LoginDTO（账号userName和password和rememberMe）
        AllUser userMasge = allUserService.findByPhoneNumber(phoneNumber);
        if (userMasge == null) {
            return ResponseData.fail(400, "用户不存在");
        }
        LoginDTO loginDTO = new LoginDTO(userMasge.getUserName(), userMasge.getPassword(), false);

        return ResponseData.ok(loginDTO);
    }

    /**
     * 注册
     * @param phoneNumber
     * @param inputCode
     * @return
     */
    @PostMapping("/verify-sms-code")
    public ResponseData<String> verifyCode(@RequestParam String phoneNumber, @RequestParam String inputCode) {
        String key = "SMS_" + phoneNumber;
        String storedCode = redisTemplate.opsForValue().get(key);
        if (storedCode == null) {
            return ResponseData.fail(400, "验证码已过期或不存在");
        }
        if (!storedCode.equals(inputCode)) {
            return ResponseData.fail(400, "验证码不正确");
        }
        return ResponseData.ok("验证码通过");
    }

}
